﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace WpfApp2
{
    public class RequiredRule : ValidationRule
    {
        private int minNumLength = -1;
        private int maxNmubLength = -1;
        private string errorMessage;
        public int MinNumLength
        {
            get { return minNumLength; }
            set { minNumLength = value; }
        }
        public int MaxNumLenght
        {
            get { return maxNmubLength; }
            set { maxNmubLength = value; }
        }
        public string ErrorMessage
        {
            get { return errorMessage; }
            set { errorMessage = value; }
        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            ValidationResult result = new ValidationResult(true, null);
            if (value == null)
                result = new ValidationResult(false, "不能为空值");
            if(string.IsNullOrEmpty(value.ToString()))
                result = new ValidationResult(false, "不能为空字符串");
            string inputstring = (value ?? string.Empty).ToString();
            if (inputstring.Length < this.minNumLength || (this.maxNmubLength > 0 && inputstring.Length > this.maxNmubLength))
                result = new ValidationResult(false, this.errorMessage);
            return result;

        }
    }
}
